package com.gopay.scheduler.job;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.cps.dao.bank.BisBankInfoMainDAO;
import com.gopay.common.cps.dao.wallet.creditCard.CardRepaymentDao;
import com.gopay.common.domain.UserInfo;
import com.gopay.common.domain.bis.BisBankInfoMain;
import com.gopay.common.domain.dps.CardRepayment;
import com.gopay.common.domain.wamsgsend.WaMsgSendInVo;
import com.gopay.common.exception.GopayException;
import com.gopay.common.user.dao.UserInfoDAO;
import com.gopay.remote.notify.WaMsgSendRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.util.DateUtils;
/**
 * 行用卡还款提醒
 * @author TangYang
 *
 */
public class CreditCardRepaymentRemindJob extends GopayBaseJob {
	private Logger logger = LoggerFactory.getLogger(CreditCardRepaymentRemindJob.class);
	
	@Resource(name = "commonCardRepaymentDao")
	private CardRepaymentDao cardRepaymentDao;
	
	@Resource(name = "bisBankInfoMainDAO")
	private BisBankInfoMainDAO bisBankInfoMainDAO;
	
	@Resource(name = "commonQueryUserDao")
	private UserInfoDAO userInfoDAO;
	
	@Resource(name="waMsgSendRemoteService")
	private WaMsgSendRemoteService waMsgSendService;
	
	@Override
	protected void doJobInternal() {
		try{
		logger.info("---行用卡还款提醒开始");
		// TODO Auto-generated method stub
		//获取当前日期
		String currDate = DateUtils.getCurrDate(DateUtils.yyyyMMdd);
		logger.info("------获取当前日期currDate="+currDate);
		//查询WA_CARD_REPAYMENT表，开关开启且还款日为今天的数据
		List<CardRepayment> cardRepaymentList = cardRepaymentDao.queryByRepaymenDayAndRepayme(new Long(currDate.substring(6)), new Long(1));
		if(cardRepaymentList.size()>0){
			for(CardRepayment cardRepayment:cardRepaymentList){
				String bankName = null;
				String userId = null;
				String bankCode = cardRepayment.getBankCode();
				String custId = cardRepayment.getCustId();
				if(bankCode!=null&&!"".equals(bankCode)){
					BisBankInfoMain  bisBankInfoMain= bisBankInfoMainDAO.getByCode(bankCode);
					if(bisBankInfoMain!=null){
						bankName = bisBankInfoMain.getBankName();
					}
				}
				String bankAcctNum = cardRepayment.getBankAcctNum();
				if(bankAcctNum.length()>4){
					bankAcctNum = bankAcctNum.substring(bankAcctNum.length()-4);
				}
				String text="今天是"+bankName+"（"+bankAcctNum+"）还款提醒日，记得及时还款哦！";
				List<UserInfo> userInfoList = userInfoDAO.getByCustId(custId);
				if(userInfoList!=null && userInfoList.size()>0){
					userId = userInfoList.get(0).getUserId();
				}
				sendMassage("还款提醒", "还款提醒", text, userId);
			}
		}
		//消息入库
		}catch (NumberFormatException e){
			logger.error("---行用卡还款提醒异常");
			e.printStackTrace();
		}catch (GopayException e){
			logger.error("---行用卡还款提醒异常");
			e.printStackTrace();
		}
	}
	/**
	 * 封装消息推送内容
	 * @param title
	 * @param ticker
	 * @param text
	 * @param userId
	 */
	private void sendMassage(String title,String ticker,String text,String userId) {
		WaMsgSendInVo rep=new WaMsgSendInVo();
		rep.setTitle(title);
		rep.setTicker(ticker);
		rep.setText(text);
		rep.setUserId(userId);
		rep.setTran_type("1");
		
		try {
			waMsgSendService.saveUnicast_WaMsgSend(rep);
		} catch (Exception e) {
			this.logger.error("消息推送失败");
			e.printStackTrace();
		}
	}
}
